package sicnu.cs.aps.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import sicnu.cs.aps.common.ResultInfo;
import sicnu.cs.aps.common.ResultInfoUtil;
import sicnu.cs.aps.common.enums.FileTypeEnum;
import sicnu.cs.aps.service.IFileUploadService;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author CaiKe
 * @date 2022/5/26 19:55
 */
@Slf4j
@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
@Api(tags = {"文件模块"})
public class FileResource {

    private final IFileUploadService fileUploadService;

    @PostMapping("/text/upload")
    @ApiOperation("富文本上传图片")
    public ResultInfo<String> uploadFile(MultipartFile file) throws IOException {
        final String path = fileUploadService.uploadFile((FileInputStream) file.getInputStream(), FileTypeEnum.BLOG, file.getOriginalFilename());
        return ResultInfoUtil.buildSuccessWithStr(path);
    }
}